﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class QuanTri_QuanLyTinTuc : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                LoadChuyenMuc();
                rdoHienThi.Checked = true;
                LayDuLieu();
                TangMa1();
                txtNgayDang.Text = DateTime.Today.ToShortDateString();
            }
        }
    }
    void LayDuLieu()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("MaTin",typeof(string));
        dt.Columns.Add("TieuDe", typeof(string));
        dt.Columns.Add("NoiDung", typeof(string));
        dt.Columns.Add("Anh", typeof(string));
        dt.Columns.Add("NgayDang", typeof(string));
        dt.Columns.Add("TrangThaiDang", typeof(string));
        dt.Columns.Add("TenChuyenMuc",typeof(string));
        dt.Columns.Add("TomTatNoiDung",typeof(string));
        var c = from p in db.TinTucs select p;
        if (c.Count() != 0)
        {
            foreach (var c1 in c)
            {
                DataRow dr = dt.NewRow();
                dr[0] = c1.MaTin.ToString();
                dr[1] = c1.TieuDe;
                dr[2] = c1.NoiDung;
                dr[3] = c1.Anh;
                dr[4] = c1.NgayDang.Date.ToShortDateString();
                dr[5] = c1.TrangThaiDang.ToString();
                dr[6] = c1.ChuyenMucTin.TenChuyenMuc.ToString();
                dr[7] = c1.TomTatNoiDung;
                dt.Rows.Add(dr);
            }
        }
        grvtintuc.DataSource = dt;
        grvtintuc.DataBind();
    }
    void LoadChuyenMuc()
    {
        var c = from p in db.ChuyenMucTins select p;
        ddlChuyenMuc.DataTextField = "TenChuyenMuc";
        ddlChuyenMuc.DataValueField = "MaChuyenMuc";
        ddlChuyenMuc.DataSource = c;
        ddlChuyenMuc.DataBind();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        bool kt = true;
        kt = KTTruongTrong();
        if (kt == false)
        { return; }
        //if (fulAnh.HasFile)
        //{
        //    KTduongan(fulAnh.PostedFile);   
        //}
        TinTuc tt = new TinTuc();
        tt.MaTin = int.Parse(lblMaTin.Text);
        tt.TieuDe = txtTieuDe.Text;
        tt.NoiDung =FCKeditor1.Value.ToString();
        tt.NgayDang = DateTime.Parse(txtNgayDang.Text);
        tt.TomTatNoiDung = txtTomTatND.Text;
        tt.MaChuyenMuc = int.Parse(ddlChuyenMuc.SelectedValue.ToString());
        if (rdoHienThi.Checked == true)
        {
            tt.TrangThaiDang = true;
        }
        if (rdoKHienThi.Checked == true)
        {
            tt.TrangThaiDang = false;
        }
        tt.Anh = fulAnh.PostedFile.FileName;
        db.TinTucs.InsertOnSubmit(tt);
        db.SubmitChanges();
        LayDuLieu();
        TangMa1();
        refresh();
    }
    void TangMa1()
    {
        var t = from p in db.TinTucs orderby p.MaTin descending select p.MaTin;
        string tmp = t.FirstOrDefault().ToString();
        if (tmp == "")
        {
            lblMaTin.Text = "1";
        }
        else
            lblMaTin.Text = (int.Parse(tmp.Trim()) + 1).ToString();
    }
    protected void grvtintuc_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grvtintuc.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaTin");
        TinTuc tt = db.TinTucs.SingleOrDefault(p => p.MaTin == int.Parse(Ma.Text));
        txtTieuDe.Text = tt.TieuDe;
        FCKeditor1.Value = tt.NoiDung;
        txtTomTatND.Text = tt.TomTatNoiDung;
        ddlChuyenMuc.SelectedItem.Text = tt.ChuyenMucTin.TenChuyenMuc;
        txtNgayDang.Text = tt.NgayDang.Date.ToShortDateString();
        if (tt.TrangThaiDang == true)
        { rdoHienThi.Checked = true; }
        if (tt.TrangThaiDang == false)
        { rdoKHienThi.Checked = true; }
    }
    void KTduongan(HttpPostedFile file)
    {
        string path = Server.MapPath("Anh");
        string filename = fulAnh.PostedFile.FileName.Trim();
        string pathToCheck = path + @"\" + filename;
        fulAnh.SaveAs(pathToCheck);
    }
//    *.amff *.AVS *.BIFF *.BMP *.CCITT-g4 *.CCITT-t4 *.DCX *.FIG *.FITS *.GIF87a *.GIF89a
//*.GIF-comp *.GL *.IFF *.ICC *.JPG *.NAP *.PBM *.PCX *.PBMPlus *.PCX2 *.PGM *.PIX

//*.PNM *.PPM *.RAS *.RGB *.rgbtrans *.RLE *.SLB *.SLD *.TGA *.TIFF-4 *.TIFF-5

//*.TIFF-6 *.TIFF-f *.XBM *.XWD
    protected void btnSua_Click(object sender, EventArgs e)
    {
        bool kt = true;
        GridViewRow row = grvtintuc.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaTin");
        TinTuc tt = db.TinTucs.SingleOrDefault(p => p.MaTin == int.Parse(Ma.Text));
        kt = KTTruongTrong();
        if (kt == false)
        { return; }
        tt.TieuDe = txtTieuDe.Text;
        tt.NgayDang = DateTime.Parse(txtNgayDang.Text);
        tt.NoiDung = FCKeditor1.Value.ToString();
        tt.MaChuyenMuc = int.Parse(ddlChuyenMuc.SelectedValue.ToString());
        tt.TomTatNoiDung = txtTomTatND.Text;
        tt.Anh = fulAnh.PostedFile.FileName;
        if (rdoKHienThi.Checked == true)
        { tt.TrangThaiDang = false; }
        if (rdoHienThi.Checked == true)
        { tt.TrangThaiDang = true; }
        db.SubmitChanges();
        LayDuLieu();
        refresh();
    }
    bool KTTruongTrong()
    {
        bool kt = true;
        if (FCKeditor1.Value.ToString() == "" || txtTieuDe.Text == "")
        {
            kt = false;
            lblThongBao.Text = "Không được để trống thông tin nào";
        }
        return kt;
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvtintuc.SelectedRow;
        Label Ma = (Label)row.FindControl("lblMaTin");
        TinTuc tt = db.TinTucs.SingleOrDefault(p => p.MaTin == int.Parse(Ma.Text));
        db.TinTucs.DeleteOnSubmit(tt);
        db.SubmitChanges();
        LayDuLieu();
    }
    void refresh()
    {
        txtTomTatND.Text = "";
        txtTieuDe.Text = "";
        FCKeditor1.Value = "";
        txtNgayDang.Text = DateTime.Today.ToShortDateString();
        rdoHienThi.Checked = true;
    }
    protected void grvtintuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvtintuc.PageIndex = e.NewPageIndex;
        LayDuLieu();
    }
}
